package com.zretc.device.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zretc.dbtools.DBTools;
import com.zretc.device.entity.User;

public class UserDao {
	/**
	 * 添加用户
	 * 
	 * @param user user对象
	 * @return true 添加成功
	 */
	public boolean addUser(User user) {
		// 调用工具
		String sql = "insert into user values (?,?,?)";
		int n = DBTools.getUpdate(sql, user.getUsername(),user.getUsercode(), user.getPassword());
		return n > 0;
	}

	/**
	 * 删除用户
	 * 
	 * @param user user对象
	 * @return true 删除成功
	 */
	public boolean delUser(String userName) {
		// 调用工具
		String sql = "delete from user where username=?";
		int n = DBTools.getUpdate(sql, userName);
		return n > 0;
	}

	/**
	 * 判断用户输入是否正确
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean dologin(String username, String password) {
		int n = 0;
		// 调用工具
		String sql = "select count(username) from user where username=? and password=?";
		ResultSet rs = DBTools.getQuery(sql, username, password);
		try {
			// 遍历
			if (rs.next()) {
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return n == 1;
	}

	/**
	 * 查询所有
	 * 
	 * @return
	 */
	public List<User> findUserAll() {
		// 创建集合
		List<User> list = new ArrayList<User>();
		String sql = "select * from user";
		ResultSet rs = DBTools.getQuery(sql, null);
		try {
			while (rs.next()) {
				// 用户名
				String username = rs.getString(1);
				// 昵称
				String usercode = rs.getString(2);
				// 密码
				String password = rs.getString(3);
				User user = new User(username, usercode, password);
				// 添加到集合中
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * 根据用户名查询
	 * 
	 * @return
	 */
	public User findUserById(String userName) {
		// 创建对象
		User user =null;
		String sql = "select * from user where username=?";
		ResultSet rs = DBTools.getQuery(sql, userName);
		try {
			if (rs.next()) {
				// 用户名
				String username = rs.getString(1);
				// 昵称
				String usercode = rs.getString(2);
				// 密码
				String password = rs.getString(2);
				user = new User(userName, usercode, password);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}
	/**
	 * 修改密码的方法
	 * @return
	 */
	public boolean modUser(User user) {
		String sql="update user set password=? where username=?";
		int n=DBTools.getUpdate(sql, user.getPassword(),user.getUsername());
		return n>0;
	}

}
